using Lzfy_Refund_Service.Models;
using Lzfy_Refund_Service.Repositories;

namespace Lzfy_Refund_Service.Services
{
    public class RoleService : IRoleService
    {
        private readonly IRoleRepository _roleRepository;
        private readonly IPermissionRepository _permissionRepository;

        public RoleService(IRoleRepository roleRepository, IPermissionRepository permissionRepository)
        {
            _roleRepository = roleRepository;
            _permissionRepository = permissionRepository;
        }

        public async Task<RoleListResponse> GetRoleListAsync(RoleListRequest request)
        {
            return await _roleRepository.GetRoleListAsync(request);
        }

        public async Task<RoleDto?> GetRoleByIdAsync(int id)
        {
            var role = await _roleRepository.GetRoleByIdAsync(id);
            if (role == null) return null;

            return new RoleDto
            {
                Id = role.Id,
                InputDate = role.InputDate,
                RoleName = role.RoleName,
                RoleCode = role.RoleCode,
                Description = role.Description,
                IsActive = role.IsActive,
                SortOrder = role.SortOrder
            };
        }

        public async Task<int> CreateRoleAsync(RoleDto roleDto)
        {
            // 验证角色代码是否已存在
            if (await _roleRepository.RoleCodeExistsAsync(roleDto.RoleCode))
            {
                throw new InvalidOperationException("角色代码已存在");
            }

            var role = new Role
            {
                RoleName = roleDto.RoleName,
                RoleCode = roleDto.RoleCode,
                Description = roleDto.Description,
                IsActive = roleDto.IsActive,
                SortOrder = roleDto.SortOrder
            };

            return await _roleRepository.AddRoleAsync(role);
        }

        public async Task<bool> UpdateRoleAsync(int id, RoleDto roleDto)
        {
            // 验证角色是否存在
            var existingRole = await _roleRepository.GetRoleByIdAsync(id);
            if (existingRole == null)
            {
                throw new InvalidOperationException("角色不存在");
            }

            // 验证角色代码是否已被其他角色使用
            if (await _roleRepository.RoleCodeExistsAsync(roleDto.RoleCode, id))
            {
                throw new InvalidOperationException("角色代码已存在");
            }

            var role = new Role
            {
                Id = id,
                RoleName = roleDto.RoleName,
                RoleCode = roleDto.RoleCode,
                Description = roleDto.Description,
                IsActive = roleDto.IsActive,
                SortOrder = roleDto.SortOrder
            };

            return await _roleRepository.UpdateRoleAsync(role);
        }

        public async Task<bool> DeleteRoleAsync(int id)
        {
            // 验证角色是否存在
            var existingRole = await _roleRepository.GetRoleByIdAsync(id);
            if (existingRole == null)
            {
                throw new InvalidOperationException("角色不存在");
            }

            return await _roleRepository.DeleteRoleAsync(id);
        }

        public async Task<bool> ToggleRoleStatusAsync(int id, bool isActive)
        {
            // 验证角色是否存在
            var existingRole = await _roleRepository.GetRoleByIdAsync(id);
            if (existingRole == null)
            {
                throw new InvalidOperationException("角色不存在");
            }

            return await _roleRepository.ToggleRoleStatusAsync(id, isActive);
        }

        public async Task<List<RoleDto>> GetAllActiveRolesAsync()
        {
            return await _roleRepository.GetAllActiveRolesAsync();
        }

        public async Task<bool> AssignRolePermissionsAsync(RolePermissionRequest request)
        {
            // 验证角色是否存在
            var existingRole = await _roleRepository.GetRoleByIdAsync(request.RoleId);
            if (existingRole == null)
            {
                throw new InvalidOperationException("角色不存在");
            }

            // 验证菜单是否都存在
            foreach (var menuId in request.MenuIds)
            {
                var menu = await _permissionRepository.GetMenuByIdAsync(menuId);
                if (menu == null)
                {
                    throw new InvalidOperationException($"菜单ID {menuId} 不存在");
                }
            }

            return await _permissionRepository.AssignRolePermissionsAsync(request.RoleId, request.MenuIds);
        }

        public async Task<List<MenuDto>> GetRolePermissionsAsync(int roleId)
        {
            // 验证角色是否存在
            var existingRole = await _roleRepository.GetRoleByIdAsync(roleId);
            if (existingRole == null)
            {
                throw new InvalidOperationException("角色不存在");
            }

            return await _permissionRepository.GetRoleMenusAsync(roleId);
        }
    }
}